package com.example.demo1101.demo2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SellTicketLock {


    public static void main(String[] args) throws InterruptedException {

        //多线程操作 多个线程操作同一个资源类

        Ticket01 ticket = new Ticket01();

        Thread t1 = new Thread(ticket, "A窗口");
        Thread t2 = new Thread(ticket, "B窗口");
        Thread t3 = new Thread(ticket, "C窗口");
        Thread t4 = new Thread(ticket, "D窗口");
        t1.start();
        t2.start();
        t2.join();
        t3.start();
        t4.start();
    }
}

class Ticket02 implements Runnable {
    //属性 方法
    private int number = 50;
    Lock lock = new ReentrantLock();

    //买票的方式 手动 锁
    public void run() {
        lock.lock();
        try {


            for (int i = 0; i <= number + 2; i++) {
                System.out.println(Thread.currentThread().getName() + "卖出了第" + (50 - (--number)) + "票，剩余" + number);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

